<template>
  <div class="login-container">
    <!-- 梦幻动态背景 -->
    <div class="dream-background">
      <!-- 渐变光晕 -->
      <div class="gradient-orb orb1"></div>
      <div class="gradient-orb orb2"></div>
      <div class="gradient-orb orb3"></div>

      <!-- 流星效果 -->
      <div class="meteor-shower">
        <div
          v-for="n in 8"
          :key="n"
          class="meteor"
          :style="{
            '--delay': `${Math.random() * 2}s`,
            '--duration': `${Math.random() * 2 + 1}s`,
            '--top': `${Math.random() * 40}%`,
            '--left': `${Math.random() * 100}%`,
          }"
        ></div>
      </div>

      <!-- 动态粒子 -->
      <div class="particles-container">
        <div
          v-for="n in 50"
          :key="n"
          class="particle"
          :style="{
            '--delay': `${Math.random() * 15}s`,
            '--size': `${Math.random() * 4 + 1}px`,
            '--start-x': `${Math.random() * 100}%`,
            '--start-y': `${Math.random() * 100}%`,
            '--color': `hsl(${Math.random() * 60 + 20}, 80%, 70%)`,
          }"
        ></div>
      </div>
    </div>

    <div class="login-box">
      <div class="login-form">
        <h2>登录</h2>
        <a-form :model="formState" name="basic" autocomplete="off" @finish="handleSubmit">
          <div>
            <a-form-item name="userAccount" :rules="[{ required: true, message: '请输入账号' }]">
              <a-input v-model:value="formState.userAccount" placeholder="请输入账号" />
            </a-form-item>

            <a-form-item
              name="userPassword"
              :rules="[
                { required: true, message: '请输入密码' },
                { min: 6, message: '密码长度不能小于 6 位' },
              ]"
            >
              <a-input-password v-model:value="formState.userPassword" placeholder="请输入密码" />
            </a-form-item>
          </div>
          <div class="form-footer">
            <a class="forgot-password" @click="handleSubmit">忘记密码?</a>
            <a-button type="primary" html-type="submit" class="login-button"> 登录 </a-button>
          </div>
        </a-form>
      </div>
      <div class="register-box">
        <h3 style="font-size: 35px">没有帐号?</h3>
        <p style="margin-top: 30px">立即注册吧 😊</p>
        <RouterLink to="/user/register">
          <a-button class="register-button"> 注册 </a-button>
        </RouterLink>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { reactive, onMounted } from 'vue'
import { userLoginUsingPost } from '@/api/userController'
import { useLoginUserStore } from '@/stores/useLoginUserStore'
import { message } from 'ant-design-vue'
import { useRouter, useRoute } from 'vue-router'

// 获取路由实例和当前路由
const router = useRouter()
const route = useRoute()

// 用于接受表单输入的值
const formState = reactive({
  userAccount: '',
  userPassword: '',
})

const loginUserStore = useLoginUserStore()

/**
 * 提交表单
 * @param values
 */
const handleSubmit = async (values) => {
  const res = await userLoginUsingPost(values)
  // 登录成功，把登录态保存到全局状态中
  if (res.data.code === 0 && res.data.data) {
    await loginUserStore.fetchLoginUser()
    message.success('登录成功')

    // 获取重定向地址
    const redirectPath = route.query.redirect

    // 如果有重定向地址，则跳转到该地址，否则跳转到主页
    router.replace(redirectPath ? redirectPath.toString() : '/')
  } else {
    message.error('登录失败，' + res.data.message)
  }
}

// 检查是否已登录，如果已登录则直接跳转到主页
onMounted(() => {
  if (loginUserStore.loginUser.id) {
    router.replace('/')
  }
})
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  position: relative;
  overflow: hidden;
  padding: 20px;
}

/* 梦幻背景 */
.dream-background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #fff6f0, #ffe4d4);
  overflow: hidden;
}

/* 渐变光晕 */
.gradient-orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(80px);
  opacity: 0.4;
  mix-blend-mode: soft-light;
  animation: floatOrb 20s infinite ease-in-out;
}

.orb1 {
  width: 500px;
  height: 500px;
  background: radial-gradient(circle at 30% 30%, #ff9f43, rgba(255, 138, 101, 0));
  top: -10%;
  left: -10%;
  animation-duration: 25s;
}

.orb2 {
  width: 600px;
  height: 600px;
  background: radial-gradient(circle at 70% 30%, #ffd6a5, rgba(255, 214, 165, 0));
  bottom: -20%;
  right: -10%;
  animation-duration: 30s;
  animation-delay: 2s;
}

.orb3 {
  width: 450px;
  height: 450px;
  background: radial-gradient(circle at 50% 50%, #ffecb3, rgba(255, 236, 179, 0));
  top: 40%;
  left: 30%;
  animation-duration: 28s;
  animation-delay: 5s;
}

/* 流星效果 */
.meteor-shower {
  position: absolute;
  width: 100%;
  height: 100%;
}

.meteor {
  position: absolute;
  top: var(--top);
  left: var(--left);
  width: 2px;
  height: 2px;
  background: #ffa502;
  border-radius: 50%;
  animation: meteor var(--duration) linear infinite;
  animation-delay: var(--delay);
}

.meteor::before {
  content: '';
  position: absolute;
  width: 100px;
  height: 1px;
  transform: translateX(-100%);
  background: linear-gradient(90deg, #ffa502, transparent);
}

/* 动态粒子 */
.particles-container {
  position: absolute;
  width: 100%;
  height: 100%;
}

.particle {
  position: absolute;
  width: var(--size);
  height: var(--size);
  border-radius: 50%;
  background: var(--color);
  left: var(--start-x);
  top: var(--start-y);
  animation: floatParticle var(--delay) infinite linear;
  opacity: 0.6;
}

/* 登录框样式优化 */
.login-box {
  position: relative;
  display: flex;
  border-radius: 20px;
  overflow: hidden;
  width: 800px;
  min-height: 500px;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(31, 38, 135, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.2);
  animation: loginBoxFadeIn 0.8s ease-out;
}

.login-form {
  flex: 1;
  padding: 120px 50px 50px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
}

.register-box {
  flex: 1;
  /* background: rgba(255, 77, 79, 0.8); */
  background: linear-gradient(135deg, rgba(242, 110, 112, 0.9) 0%, rgba(255, 19, 101, 0.9) 100%);
  backdrop-filter: blur(7px);
  color: white;
  padding: 50px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

h2 {
  margin-bottom: 10px;
  color: #333;
  font-size: 28px;
}

/* h3 {
  margin-bottom: 30px;
  color: #ffffff;
  font-size: 30px;
} */

.form-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 24px;
}

.forgot-password {
  color: #666;
  transition: color 0.3s;
}

.forgot-password:hover {
  color: #ff4d4f;
}

.login-button {
  background: linear-gradient(135deg, #ff9f43, #ff7f50);
  border: none;
  width: 120px;
  height: 40px;
  font-size: 16px;
  border-radius: 20px;
  transition: all 0.3s ease;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(255, 159, 67, 0.3);
}

.register-button {
  background: transparent;
  border: 2px solid white;
  color: white;
  margin-top: 20px;
  height: 40px;
  width: 140px;
  font-size: 16px;
  border-radius: 20px;
  transition: all 0.3s;
}

.register-button:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-2px);
}

.register-box h3 {
  color: white;
  margin-bottom: 12px;
  font-size: 24px;
}

.register-box p {
  color: white;
  margin-bottom: 24px;
  font-size: 16px;
}

/* 修改输入框样式 */
:deep(.ant-input),
:deep(.ant-input-password) {
  height: 40px;
  border-radius: 20px;
  padding: 4px 16px; /* 添加内边距 */
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid rgba(255, 159, 67, 0.2);
  transition: all 0.3s ease;
}

:deep(.ant-input-password .ant-input) {
  padding-right: 40px; /* 为密码框图标留出空间 */
}

:deep(.ant-input-affix-wrapper) {
  padding: 0 12px; /* 调整密码框容器的内边距 */
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.9);
}

/* 动画效果 */
@keyframes meteor {
  0% {
    transform: rotate(-45deg) translateX(0);
    opacity: 1;
  }
  100% {
    transform: rotate(-45deg) translateX(1000px);
    opacity: 0;
  }
}

@keyframes floatParticle {
  0% {
    transform: translate(0, 0) rotate(0deg);
    opacity: 0;
  }
  20% {
    opacity: 0.6;
  }
  80% {
    opacity: 0.6;
  }
  100% {
    transform: translate(calc(sin(var(--delay)) * 200px), calc(cos(var(--delay)) * 200px))
      rotate(360deg);
    opacity: 0;
  }
}

@keyframes floatOrb {
  0%,
  100% {
    transform: translate(0, 0) scale(1);
  }
  25% {
    transform: translate(-30px, 30px) scale(1.1);
  }
  50% {
    transform: translate(30px, -20px) scale(0.9);
  }
  75% {
    transform: translate(20px, 30px) scale(1.05);
  }
}

@keyframes loginBoxFadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式调整 */
@media (max-width: 768px) {
  .login-box {
    flex-direction: column;
    width: 100%;
    max-width: 500px;
  }

  .login-form,
  .register-box {
    width: 100%;
    padding: 30px;
  }
}
</style>
